


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Utility_Asset_Meters_Images') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Utility_Asset_Meters_Images
GO

create view v_tb_Utility_Asset_Meters_Images as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Utility_Asset_Meters_ImageId] as [Utility_Asset_Meters_ImageId]
	,[Utility_Asset_MeterFK] as [Utility_Asset_MeterFK]
	,[Direction] as [Direction]
	,[Latitude] as [Latitude]
	,[Longitude] as [Longitude]
	,[Altitude] as [Altitude]
	,[SatelliteCount] as [SatelliteCount]
	,[SatelliteTime] as [SatelliteTime]
	,[SolutionAvailable] as [SolutionAvailable]
	,[Filename] as [Filename]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Filename]), 255) as [FriendlyName]
from
	tb_Utility_Asset_Meters_Images
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Test
GO


create proc up_UtilityAssetMetersImage_Test
	@Utility_Asset_Meters_ImageId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Utility_Asset_Meters_ImageId] 
	from [tb_Utility_Asset_Meters_Images] source
	where [source].[Utility_Asset_Meters_ImageId] = @Utility_Asset_Meters_ImageId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Select
GO

create proc up_UtilityAssetMetersImage_Select
	 @Utility_Asset_Meters_ImageId int = null 
	,@Utility_Asset_MeterFK int = null 
	,@Direction float = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Filename nvarchar(200) = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select * from v_tb_Utility_Asset_Meters_Images
where
		IsNull([v_tb_Utility_Asset_Meters_Images].[Utility_Asset_Meters_ImageId], -1) = Coalesce(@Utility_Asset_Meters_ImageId, [v_tb_Utility_Asset_Meters_Images].[Utility_Asset_Meters_ImageId], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Utility_Asset_MeterFK], -1) = Coalesce(@Utility_Asset_MeterFK, [v_tb_Utility_Asset_Meters_Images].[Utility_Asset_MeterFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Direction], -1) = Coalesce(@Direction, [v_tb_Utility_Asset_Meters_Images].[Direction], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_Meters_Images].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_Meters_Images].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_Meters_Images].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_Meters_Images].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_Meters_Images].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_Meters_Images].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Filename], '') = Coalesce(@Filename, [v_tb_Utility_Asset_Meters_Images].[Filename], '')
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Select_Top1
GO

create proc up_UtilityAssetMetersImage_Select_Top1
	 @Utility_Asset_Meters_ImageId int = null 
	,@Utility_Asset_MeterFK int = null 
	,@Direction float = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Filename nvarchar(200) = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected top 1 record based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select TOP 1 * from v_tb_Utility_Asset_Meters_Images
where
		IsNull([v_tb_Utility_Asset_Meters_Images].[Utility_Asset_Meters_ImageId], -1) = Coalesce(@Utility_Asset_Meters_ImageId, [v_tb_Utility_Asset_Meters_Images].[Utility_Asset_Meters_ImageId], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Utility_Asset_MeterFK], -1) = Coalesce(@Utility_Asset_MeterFK, [v_tb_Utility_Asset_Meters_Images].[Utility_Asset_MeterFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Direction], -1) = Coalesce(@Direction, [v_tb_Utility_Asset_Meters_Images].[Direction], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_Meters_Images].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_Meters_Images].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_Meters_Images].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_Meters_Images].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_Meters_Images].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_Meters_Images].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_Meters_Images].[Filename], '') = Coalesce(@Filename, [v_tb_Utility_Asset_Meters_Images].[Filename], '')
GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Insert
GO

create proc up_UtilityAssetMetersImage_Insert
	 @Utility_Asset_Meters_ImageId int =null OUTPUT  
	,@Utility_Asset_MeterFK int  
	,@Direction float = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit  
	,@Filename nvarchar(200)  
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Utility_Asset_Meters_Images 
	(
	
	 [Utility_Asset_MeterFK] 
	,[Direction] 
	,[Latitude] 
	,[Longitude] 
	,[Altitude] 
	,[SatelliteCount] 
	,[SatelliteTime] 
	,[SolutionAvailable] 
	,[Filename] 
	)
	values
	(
	
	 @Utility_Asset_MeterFK 
	,@Direction 
	,@Latitude 
	,@Longitude 
	,@Altitude 
	,@SatelliteCount 
	,@SatelliteTime 
	,@SolutionAvailable 
	,@Filename 
	)
set @Utility_Asset_Meters_ImageId = scope_identity()
select * from v_tb_Utility_Asset_Meters_Images where Utility_Asset_Meters_ImageId = @Utility_Asset_Meters_ImageId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Update
GO


create proc up_UtilityAssetMetersImage_Update
	 @Utility_Asset_Meters_ImageId int  
	,@Utility_Asset_MeterFK int = null  
	,@Direction float = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit = null  
	,@Filename nvarchar(200) = null  
	,@OriginalUtility_Asset_Meters_ImageId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalUtility_Asset_Meters_ImageId is null
	set @OriginalUtility_Asset_Meters_ImageId = @Utility_Asset_Meters_ImageId

-- test
declare @Result bit
execute @Result = up_UtilityAssetMetersImage_Test @OriginalUtility_Asset_Meters_ImageId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_Meters_ImageId)
	raiserror(505060, 16, 1, 'UtilityAssetMetersImage', @x)
	return
end

-- proceed
update tb_Utility_Asset_Meters_Images 
set

	 [Utility_Asset_MeterFK] = IsNull(@Utility_Asset_MeterFK, [source].[Utility_Asset_MeterFK])
	,[Direction] =  @Direction
	,[Latitude] =  @Latitude
	,[Longitude] =  @Longitude
	,[Altitude] =  @Altitude
	,[SatelliteCount] =  @SatelliteCount
	,[SatelliteTime] =  @SatelliteTime
	,[SolutionAvailable] = IsNull(@SolutionAvailable, [source].[SolutionAvailable])
	,[Filename] = IsNull(@Filename, [source].[Filename])
	
from tb_Utility_Asset_Meters_Images source
where
	[source].[Utility_Asset_Meters_ImageId] = @OriginalUtility_Asset_Meters_ImageId


select * from v_tb_Utility_Asset_Meters_Images where Utility_Asset_Meters_ImageId = @Utility_Asset_Meters_ImageId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMetersImage_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMetersImage_Delete
GO


create proc up_UtilityAssetMetersImage_Delete
	@Utility_Asset_Meters_ImageId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_UtilityAssetMetersImage_Test @Utility_Asset_Meters_ImageId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_Meters_ImageId)
	raiserror(505060, 16, 1, 'UtilityAssetMetersImage', @x)
	return
end

-- proceed
delete from tb_Utility_Asset_Meters_Images 
where [Utility_Asset_Meters_ImageId] = @Utility_Asset_Meters_ImageId

GO


